<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">
            主页
        </a>
        <a>
            <cite>
                系统管理
            </cite>
        </a>
        <a>
            <cite>
                权限组管理
            </cite>
        </a>
        <a>
            <cite>
                分配权限
            </cite>
        </a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            分配权限
        </div>
        <div class="layui-card-body" style="padding: 15px;">
            <form action="" class="layui-form" lay-filter="component-form-group">
                <div id="test1" class="demo-tree demo-tree-box"></div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <div class="layui-footer">
                            <button class="layui-btn" lay-filter="component-form-demo1" lay-submit="">
                                立即提交
                            </button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    layui.use(['admin', 'form', 'helper', 'tree'], function () {
        var $        = layui.$
            , helper = layui.helper
            , tree   = layui.tree
            , form   = layui.form;

        helper.get('/auth-group/rules?id=' + layui.router().search.id, function (res) {
            let data = res.data;
            //常规用法
            tree.render({
                elem          : '#test1' //默认是点击节点可进行收缩
                , data        : data
                , id          : 'demoId1'
                , showCheckbox: true
            });
            /* 监听提交 */
            form.on('submit(component-form-demo1)', function (data) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
                if (checkedData.length == 0) {
                    helper.error('请选择一个权限吧');
                    return false;
                }
                data = {
                    id  : layui.router().search.id,
                    data: JSON.stringify(checkedData),
                };

                helper.post('/AuthGroup/saveAuths', data, function (res) {
                    helper.success(res.msg, function () {
                        window.location.hash = '/admin/group/';
                    })
                })
                return false;
            });
        })

    });
</script>